package com.huarun.module.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.huarun.common.BaseServiceImpl;
import com.huarun.common.util.Constant;
import com.huarun.module.sys.bean.Dict;
import com.huarun.module.sys.dao.DictDao;
import com.huarun.module.sys.service.IDictService;



/**
 * 
 * @author lilly
 * @catedDate 2018-04-27
 * @description 数据字典业务处理实现类
 *
 */
@Service("dictService")
public class DictServiceImpl extends BaseServiceImpl<DictDao, Dict> implements IDictService {

	@Resource
	private DictDao dictDao;

	@Override
	public List<Dict> getDictListByType(String type) {
		return dictDao.findDictListByType(type);
	}

	/**
	 * 根据类别获取数据字典列表(格式化后的数据)
	 * 
	 * @param type
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getFormatDictListByType(String type, boolean isIncludeAll) {
		List<Dict> dictList = dictDao.findDictListByType(type);
		return formatDictList(dictList, isIncludeAll);
	}

	/**
	 * 根据数据值获取数据名称
	 * 
	 * @param dictValue
	 * @return
	 */
	@Override
	public String getDictLabelByDictValue(String dictValue) {
		List<Map<String, Object>> list = dictDao.getDictLabelByDictValue(dictValue);
		if (null == list || list.size() != Constant.ONE) {
			return "";
		} else {
			return String.valueOf(list.get(Constant.ZERO).get("dictLabel"));
		}
	}

	/**
	 * 格式化列表
	 * 
	 * @param type
	 * @return
	 */
	public List<Map<String, Object>> formatDictList(List<Dict> dictList, boolean isIncludeAll) {
		if (dictList == null || dictList.size() == 0) {
			return null;
		}
		if (isIncludeAll) {
			Dict d = new Dict();
			d.setDictLabel("全部");
			d.setDictValue("");
			dictList.add(0, d);
		}
		List<Map<String, Object>> list = new ArrayList<>();
		Map<String, Object> map = null;
		for (Dict dict : dictList) {
			map = new HashMap<>();
			map.put("id", dict.getDictValue());
			map.put("label", dict.getDictLabel());
			list.add(map);
		}
		return list;
	}

}
